package com.kikt.recyclerview.loadmore

import android.content.Context
import android.os.Bundle
import android.support.annotation.IdRes
import android.support.v7.app.AppCompatActivity
import android.util.SparseArray
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import com.jaychang.srv.SimpleCell
import com.jaychang.srv.SimpleViewHolder
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity(), MainView {

    override fun onDataList(list: List<Data>) {
        logger.info("the main list : $list")
    }

    override fun createCell(data: Data): DataCell {
        return DataCell(data)
    }

    val list = ArrayList<Data>()
    val cells = ArrayList<DataCell>()

    init {

    }

    val presenter by lazy { MainPresenter(this) }

    override val helper by lazy { SimpleRecyclerViewHelper(srv, this, presenter, refresh_layout) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        helper.getFirstData()
    }

    class DataCell(item: Data) : SimpleCell<Data, ViewHolder>(item) {
        override fun getLayoutRes() = android.R.layout.activity_list_item

        override fun onCreateViewHolder(parent: ViewGroup, cellView: View): ViewHolder {
            return ViewHolder(cellView)
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int, context: Context, payload: Any?) {
            holder.getView<TextView>(android.R.id.text1).text = "$position : text = ${item.text} payLoad = $payload"
        }

    }

    class ViewHolder(itemView: View) : SimpleViewHolder(itemView)
}

data class Data(val position: Int, val text: String)

val SimpleViewHolder.viewMap: SparseArray<View> by lazy {
    SparseArray<View>()
}

fun <V : View> SimpleViewHolder.getView(@IdRes id: Int): V {
    return itemView.findViewById(id)
}

fun <V : TextView> SimpleViewHolder.getTextView(@IdRes id: Int): V {
    return itemView.findViewById(id)
}

fun <V : EditText> SimpleViewHolder.getEditText(@IdRes id: Int): V {
    return itemView.findViewById(id)
}

fun <V : ImageView> SimpleViewHolder.getImageView(@IdRes id: Int): V {
    return itemView.findViewById(id)
}